package com.hjh.jpademo;

import com.hjh.jpademo.entity.Role;
import com.hjh.jpademo.entity.User;
import com.hjh.jpademo.repository.RoleRepository;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import javax.annotation.Resource;

@RunWith(SpringRunner.class)
@SpringBootTest
public class RoleTester {

    @Resource
    private RoleRepository roleRepository;

    @Test
    public void testGet() { //测试按主键查询角色，并输出关联的用户数量
        Role role = roleRepository.findById(1L).get();
        System.out.println("roleName：" + role.getRoleName());
        System.out.println("users.size：" + role.getUsers().size());
    }

    @Test
    public void testAdd() { //测试级联新增
        Role role = new Role("测试角色","演示级联新增角色和用户",1);
        User user1 = new User("测试用户1","123456",role,1);
        User user2 = new User("测试用户2","123456",role,1);
        role.getUsers().add(user1);
        role.getUsers().add(user2);

        roleRepository.save(role);
    }

    @Test
    public void testDelete() {
        Role role = roleRepository.getOne(9L);
        roleRepository.delete(role);
    }
}
